<?php

namespace App\Http\Controllers\home\seller;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class OrderController extends Controller
{
    /**
     *  卖家中心  订单管理
     */
    public function getIndex(Request $request)
    {
        // 接受搜索的关键字
        $search = $request -> input('search','');
       
        // 接受每页显示的条数
        $count = $request -> input('count',10);

        // 查询当前用户的订单信息
        $res = DB::table('my_order') -> where('merchant','=',session('homeusername')) -> where('goodsname','like','%'.$search.'%') -> orderBy('ctime','desc')  -> paginate($count);
      
        // 查询商品表的信息
        $goods= DB::table('goods_basis') -> get();

        $data = []; 
        // 将两个表的信息拼接成一个数组
        foreach($res as $v){
            foreach ($goods as  $va) {
                if($v['gid'] == $va['id']){
                   
                    $data[] = array_merge ($va,$v);
                }
            }
        }
        
        // 统计条数
        $num = DB::table('my_order') -> where('merchant','=',session('homeusername')) -> where('goodsname','like','%'.$search.'%') -> count();
       
        // 加载模板 并传数据
        return view('/home/seller/order/index',['res'=>$res,'data'=>$data,'num'=>$num,'request' => $request -> all()]);
    }


    /**
    *   卖家中心 接受订单和退款管理
    */
    public function getManage(Request $request)
    {
        // 接收要处理订单的ID和订单状态
        $arr = $request -> all();

        // 处理时间
        $time = time();

        // 修改数据库里的状态
        $ress = DB::table('my_order') -> where('id','=',$arr['id']) -> update(['merstatus'=>$arr['merstatus'],'errctime'=>$time]);

        // 判断修改的结果
        if ($arr['merstatus'] == 1) {
            if ($ress) {

                $res = DB::table('my_order') -> where('id','=',$arr['id']) -> first();

                $res2 = DB::table('goods_basis') -> where('id','=',$res['gid']) -> first();
                
                // 修改商品的库存和销量
                DB::table('goods_basis') -> where('id','=',$res['gid']) -> update(['inventory'=>$res2['inventory']-$res['num'],'sales'=>$res2['sales']+$res['num']]);

                // 往订单日志数据表插入状态变更
                $data = ['pid'=>$res['id'],'customer'=>$res['subuser'],'customerid'=>$res['subid'],'seller'=>$res['merchant'],'sellerid'=>$res['merid'],'time'=>$time,'content'=>'商家已发货','status'=>1];
                DB::table('my_order_log') -> insert($data);
                
                return redirect('/home/seller/order/index') -> with('success','订单处理成功');
            } else {
                return back() -> with('error','订单处理失败');
            }
        } elseif ($arr['merstatus'] == 3) {
             if ($ress) {
                $res = DB::table('my_order') -> where('id','=',$arr['id']) -> first();

                // 往日志表添加信息
                $data = ['pid'=>$res['id'],'customer'=>$res['subuser'],'customerid'=>$res['subid'],'seller'=>$res['merchant'],'sellerid'=>$res['merid'],'time'=>$time,'content'=>'卖家处理退款中','status'=>3];
                DB::table('my_order_log') -> insert($data);
                
                // 开启事务
                DB::beginTransaction();
                
                // 查询卖家的余额并退款
                $array1 = DB::table('vault') -> where('uid','=',$res['merid']) ->first();
                $res1 = DB::table('vault') -> where('uid','=',$res['merid']) ->update(['money'=>$array1['money']-$res['price']]);
                
                // 查询买家的余额并退款
                $array2 = DB::table('vault') -> where('uid','=',$res['subid']) ->first();
                $res2 = DB::table('vault') -> where('uid','=',$res['subid']) ->update(['money'=>$array2['money']+$res['price']]);
                
                // 判断处理结果
                if($res1 && $res2) {
                    DB::commit();
                    $res = 1;
                } else {
                    DB::rollback();
                }

                return redirect('/home/seller/order/index') -> with('success','退款处理成功');
            } else {
                return back() -> with('error','退款处理失败');
            }
        }

    }


    /**
    *   卖家中心 订单日志
    */
    public function getOdetail(Request $request)
    {
        // 接受订单的ID
        $id = $request -> input('id');

        $data = DB::table('my_order_log') -> where('pid','=',$id) -> orderBy('time','asc') -> get();

        return view('home.seller.order.odetail',['data'=>$data]);
    }
}
